package com.xiamaliu.spring.webmvc.servlet;

import lombok.NoArgsConstructor;

import java.io.File;
import java.util.Locale;

@NoArgsConstructor
public class GPViewResolver {

    private final String DEFAULT_TEMPLATE_SUFFIX = ".html";

    private File templateRootDir;

    public GPViewResolver(String templateRoot) {
        String templateRootPath = this.getClass().getClassLoader().getResource(templateRoot).getFile();
        templateRootDir = new File(templateRootPath);
    }

    public GPView resolveViewName(String viewName, Locale locale) {
        if (viewName == null || "".equals(viewName.trim())) {
            return null;
        }
        viewName = viewName.endsWith(DEFAULT_TEMPLATE_SUFFIX) ? viewName : (viewName + DEFAULT_TEMPLATE_SUFFIX);
        File templdateFile = new File(((templateRootDir.getPath()) + "/" + viewName).replaceAll("/+", "/"));
        return new GPView(templdateFile);
    }
}
